package com.amigo.online.provider;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.DefaultJedisClientConfigurationBuilder;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;


@Configuration
public class RedisConfig {

    @Value("${spring.redis.user.host}")
    private String userHost;
    @Value("${spring.redis.user.port}")
    private int userPort;
    @Value("${spring.redis.user.password}")
    private String userPassword;


    @Value("${spring.redis.video.host}")
    private String videoHost;
    @Value("${spring.redis.video.port}")
    private int videoPort;
    @Value("${spring.redis.video.password}")
    private String videoPassword;

    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxActive;
    @Value("${spring.redis.jedis.pool.min-idle}")
    private int minIdle;
    @Value("${spring.redis.jedis.pool.max-wait}")
    private int maxWait;
    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;

    /**
     * @throws
     * @Title: userJedisConnectionFactory
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @return
     * @return: RedisConnectionFactory
     */
    @Bean
    public RedisConnectionFactory userJedisConnectionFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxWaitMillis(maxWait);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxIdle(maxIdle);
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(userHost, userPort);
        config.setPassword(RedisPassword.of(userPassword));
        DefaultJedisClientConfigurationBuilder builder = (DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();
        builder.usePooling().poolConfig(poolConfig);
        builder.connectTimeout(Duration.ofMillis(timeout));
        builder.readTimeout(Duration.ofMillis(timeout));
        JedisClientConfiguration clientConfig = builder.build();
        return new JedisConnectionFactory(config, clientConfig);
    }
	
	/*@Bean
	public RedisConnectionFactory videoJedisConnectionFactory() {
	    JedisPoolConfig poolConfig = new JedisPoolConfig();	    
	    poolConfig.setMaxTotal(maxActive);
	    poolConfig.setMaxWaitMillis(maxWait);
	    poolConfig.setMinIdle(minIdle);
	    poolConfig.setMaxIdle(maxIdle);
		RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(video_host, video_port);
		config.setPassword(RedisPassword.of(video_password));
		DefaultJedisClientConfigurationBuilder builder = (DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();
	    builder.usePooling().poolConfig(poolConfig);
	    builder.connectTimeout(Duration.ofMillis(timeout));
	    builder.readTimeout(Duration.ofMillis(timeout));
	    JedisClientConfiguration clientConfig = builder.build();
	    return new JedisConnectionFactory(config,clientConfig);
	}*/
	
	/*@Bean
	public RedisConnectionFactory jedisClusterConnectionFactory() {
	    JedisPoolConfig poolConfig = new JedisPoolConfig();	    
	    poolConfig.setMaxTotal(maxActive);
	    poolConfig.setMaxWaitMillis(maxWait);
	    poolConfig.setMinIdle(minIdle);
	    poolConfig.setMaxIdle(maxIdle);		
		DefaultJedisClientConfigurationBuilder builder = (DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();
	    builder.usePooling().poolConfig(poolConfig);
	    builder.connectTimeout(Duration.ofMillis(timeout));
	    builder.readTimeout(Duration.ofMillis(timeout));
	    JedisClientConfiguration clientConfig = builder.build();	    
	    RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(clusterNodes);
	    clusterConfig.setMaxRedirects(maxRedirects);
	    return new JedisConnectionFactory(clusterConfig,clientConfig);
	}*/

    /**
     * @throws
     * @Title: userRedisTemplate
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @return
     * @return: RedisTemplate<String   ,   Object>
     */
    @Bean("userRedisTemplate")
    @Primary
    public RedisTemplate<String, Object> userRedisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(userJedisConnectionFactory());
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置值（value）的序列化采用FastJsonRedisSerializer。
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        // 设置键（key）的序列化采用StringRedisSerializer。
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
		
	/*@Bean("videoRedisTemplate")
    public RedisTemplate<String, Object> videoRedisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(videoJedisConnectionFactory());
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置值（value）的序列化采用FastJsonRedisSerializer。
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        // 设置键（key）的序列化采用StringRedisSerializer。
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }*/
}